package upload

import (
	"context"
	"easyVolunteer/utils"
	"easyVolunteer/utils/errno"
	"fmt"
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
)

// 上传本地路径文件到对象存储
func UpLoadToOss(filePath string) (string, *errno.Errno) {
	//fmt.Println(filePath)
	putPolicy := storage.PutPolicy{
		Scope: utils.Bucket,
	}

	mac := qbox.NewMac(utils.AccessKey, utils.Secretkey)
	upToken := putPolicy.UploadToken(mac)

	ret := storage.PutRet{}
	cfg := storage.Config{
		Zone:          &storage.ZoneHuadong,
		UseHTTPS:      false,
		UseCdnDomains: false,
	}

	putExtra := storage.PutExtra{
		Params: map[string]string{
			"x:name": "github logo",
		},
	}
	upLoader := storage.NewFormUploader(&cfg)

	err := upLoader.PutFile(context.Background(), &ret, upToken, filePath, filePath, &putExtra)

	if err != nil {
		fmt.Println(err)
		return "err", errno.ErrDatabase
	}

	fmt.Println(utils.ServerHost + ret.Key)
	return utils.ServerHost + ret.Key, errno.OK
}
